package com.zjut.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjut.dao.ModelDao;
import com.zjut.domain.Model;
import com.zjut.service.impl.ModelServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ModelService implements ModelServiceImpl {
    @Autowired
    private ModelDao modelDao;

    @Override
    public boolean save(Model model) {
        return modelDao.insert(model) > 0;
    }

    @Override
    public boolean update(Model model) {
        return modelDao.updateById(model) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return modelDao.deleteById(id) > 0;
    }

    //通过ID查找
    @Override
    public Model getById(Integer id) {
        return modelDao.selectById(id);
    }

    //查找全部
    @Override
    public List<Model> getAll() {
        LambdaQueryWrapper<Model> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Model::getStatus, true);
        return modelDao.selectList(lqw);
    }

    //分页展示，以及通过名字查找并展示模板
    @Override
    public IPage<Model> getPage(Integer currentPage, Integer pageSize, Model model) {
        LambdaQueryWrapper<Model> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(model.getName()), Model::getName, model.getName());
        lqw.eq(Model::getStatus, true);
        IPage page = new Page(currentPage, pageSize);
        modelDao.selectPage(page, lqw);
        return page;
    }

}
